package me.zhengjie.modules.sync.processing.forkjoin;

import com.qiaofang.applet.common.constant.SyncTypeEnum;
import com.qiaofang.applet.constants.SyncProcessingStatusConstants;
import com.qiaofang.applet.service.property.service.SyncDataQueueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ForkJoinPool;

@Slf4j
@Service
public class PropertyForkJoinProcessingTaskMain {
    @Autowired
    private SyncDataQueueService syncDataQueueService;

    public void run(){
        int count=syncDataQueueService.countByProcessingStatusAndDataType(SyncProcessingStatusConstants.UN_HANDLE, SyncTypeEnum.SALE_AND_RENT.getSyncType());
        if(count<=0){
            return;
        }
        //计算页数
        int totalPage= (int)Math.ceil((double)count / (double)PropertyForkJoinProcessingTask.PAGE_SIZE);
        ForkJoinPool forkJoinPool= new ForkJoinPool();
        PropertyForkJoinProcessingTask task  = new PropertyForkJoinProcessingTask(1,totalPage);
        long start = System.currentTimeMillis();
        forkJoinPool.invoke(task);
        log.info("propertyForkJoinProcessingTaskMain spend time:{}ms",(System.currentTimeMillis()-start));
        forkJoinPool.shutdown();
        //删除已处理的
        syncDataQueueService.deleteByProcessingStatusAndDataType(SyncProcessingStatusConstants.HANDLED, SyncTypeEnum.SALE_AND_RENT.getSyncType());
    }
}
